package com.acwing.partition11;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/9 22:21
 */
public class AC1015摘花生 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split(" ");
        int t = Integer.parseInt(s[0]);
        while (t-- > 0) {
            s = reader.readLine().split(" ");
            int n = Integer.parseInt(s[0]), m = Integer.parseInt(s[1]);
            int[][] weight = new int[n][m];
            for (int i = 0; i < n; i++) {
                s = reader.readLine().split(" ");
                for (int j = 0; j < m; j++) {
                    weight[i][j] = Integer.parseInt(s[j]);
                }
            }
            writer.write(dynamicProgramming(weight) + "\n");
        }
        writer.flush();
    }

    private static int dynamicProgramming(int[][] weight) {
        int n = weight.length, m = weight[0].length;
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + weight[i - 1][j - 1];
            }
        }
        return dp[n][m];
    }
}
